{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "#导入工具包\n",
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "#argparse 解析命令行的模块\n",
    "import argparse\n",
    "import sys\n",
    "#导入tf样本input_data\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "FLAGS = None\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting C:/Users/dell/Desktop/ai/six-week/homework\\train-images-idx3-ubyte.gz\n",
      "Extracting C:/Users/dell/Desktop/ai/six-week/homework\\train-labels-idx1-ubyte.gz\n",
      "Extracting C:/Users/dell/Desktop/ai/six-week/homework\\t10k-images-idx3-ubyte.gz\n",
      "Extracting C:/Users/dell/Desktop/ai/six-week/homework\\t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "# 导入数据，提前下载好的，直接使用一个默认目录会自己下载\n",
    "data_dir = 'C:/Users/dell/Desktop/ai/six-week/homework'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建占位符（供接收输入数据），创建变量、先加一个隐层试下效果\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "#加入一个隐层，输入到隐层的权重为W_1,隐层到输出层的权重为W_2,隐层节点数为 800\n",
    "W_1 = tf.Variable(tf.truncated_normal([784, 800], stddev=0.1))\n",
    "W_2 = tf.Variable(tf.truncated_normal([800, 10], stddev=0.1))\n",
    "b_1 = tf.Variable(tf.zeros([800]))\n",
    "b_2 = tf.Variable(tf.zeros([10]))\n",
    "#激活函数选择 relu\n",
    "H_1 = tf.nn.relu(tf.matmul(x,W_1)) + b_1\n",
    "y = tf.matmul(H_1, W_2) + b_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义真值占位符\n",
    "y_ = tf.placeholder(tf.float32, [None, 10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "#softmax_corss_entropy_with_logits方法为softmax与交叉熵 结合的方法，先计算softmax,然后计算交叉熵\n",
    "#另外对只有一个正确答案的分类问题 sparse_softmax_cross_entropy_with_logits可以加速计算过程\n",
    "#tf.reduce_mean方法计算平均值，这里是计算交叉熵平均值\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n",
    "#不加正则化，正确率最好0.9785，\n",
    "#加入正则试下,0.0001为lambda\n",
    "regularizer=tf.contrib.layers.l2_regularizer(0.0001)\n",
    "regularization=regularizer(W_1) + regularizer(W_2)\n",
    "#损失函数由原来的交叉熵 + 正则项\n",
    "loss=cross_entropy + regularization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义随机梯度下降，学习率为0.4\n",
    "train_step = tf.train.GradientDescentOptimizer(0.4).minimize(loss)\n",
    "#创建回话\n",
    "sess = tf.Session()\n",
    "#全局变量初始化\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义每个batch的数量和迭代次数\n",
    "for _ in range(4000):\n",
    "  batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9819\n"
     ]
    }
   ],
   "source": [
    "  # Test trained model\n",
    "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                      y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
